package com.srwl.mytx.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.srwl.mytx.R;

import java.util.List;

public class MenuArrayAdapter extends ArrayAdapter {
    private LayoutInflater mInflater;

    private Context mContext;
    private int mResource;
    private int textViewResourceId;

    public MenuArrayAdapter(@NonNull Context context, int resource) {
        super(context, resource);
    }


    public MenuArrayAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull List<String> objects) {
        super(context, resource, objects);
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mResource = resource;
        this.textViewResourceId = textViewResourceId;
    }


    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        // return super.getView(position, convertView, parent);
        final View view;
        final TextView text;

        if (convertView == null) {
            view = mInflater.inflate(mResource, parent, false);
        } else {
            view = convertView;
        }
        text = view.findViewById(textViewResourceId);
        Object item = getItem(position);
        if (item instanceof CharSequence) {
            text.setText((CharSequence) item);
        } else {
            text.setText(item.toString());
        }
        if (position == getCount() - 1) {//如果是最后一个，隐藏掉分割线
            View driver = view.findViewById(R.id.place_holder);
            if (driver != null) {
                driver.setVisibility(View.GONE);
            }

        }
        return view;
    }

}
